#ifndef WEBPAGE_H
#define WEBPAGE_H
#include <functional>
#include <QWebEnginePage>

class WebPage : public QWebEnginePage
{
    Q_OBJECT

public:
    typedef std::function<bool(const QUrl&, NavigationType, bool)> AcceptNavigationFunction;

    explicit WebPage(QObject* parent = nullptr);
    explicit WebPage(QWebEngineProfile* profile, QObject* parent = nullptr);
    ~WebPage() override;

    void setAcceptNavigationFunction(AcceptNavigationFunction f)
    {
        accept_navigation_function_ = f;
    }

    void setAllowDialog(bool allow_dialog) { allow_dialog_ = allow_dialog; }
    void setAlwaysConfirm(bool always_confirm) { always_confirm_ = always_confirm; }

Q_SIGNALS:
    void aboutToShowDialog();
    void closedDialog();

protected:
    void javaScriptAlert(const QUrl& securityOrigin, const QString& msg) override;
    bool javaScriptConfirm(const QUrl& securityOrigin, const QString& msg) override;
    bool javaScriptPrompt(const QUrl& securityOrigin, const QString& msg,
                          const QString& defaultValue, QString* result) override;
    void javaScriptConsoleMessage(JavaScriptConsoleMessageLevel level, const QString& message,
                                  int lineNumber, const QString& sourceID) override;
    bool acceptNavigationRequest(const QUrl& url, NavigationType type, bool isMainFrame) override;

private:
    AcceptNavigationFunction accept_navigation_function_;
    bool allow_dialog_ = true;
    bool always_confirm_ = false;
};

#endif // WEBPAGE_H
